
package DAO;

import Utils.HibernateUtil;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.Project;
import model.User;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import validator.e;

public class ProjectDA extends BaseDAO<Project>{

    @Override
    public void save(Project project) {
     Session session = HibernateUtil.getSessionFactory().openSession(); 
     Transaction t = session.beginTransaction();
     FacesContext fc = FacesContext.getCurrentInstance();
     HttpSession sessions = (HttpSession) fc.getExternalContext().getSession(false);
     
     if(consult(project.getName())){
     try{
         session.save(project);
         t.commit();
         e.exibirMsgSucesso("Sucess");
         sessions.setAttribute("display", true);
     }catch(HibernateException err){
         e.exibirMsgErro("Error - Try Again");
         sessions.setAttribute("display", false);
     }}
     else{
         e.exibirMsgErro("Error - Duplicate Name");
         sessions.setAttribute("display", false);
     }
     session.close();
    }

    @Override
    public List<Project> list() {
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpSession sessions = (HttpSession) fc.getExternalContext().getSession(false);
    User u = (User) sessions.getAttribute("user");    
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = session.beginTransaction();
    Query query= session.createSQLQuery("select * from project p where p.user_id= :user_id").addEntity(Project.class);
    query.setParameter("user_id", u.getId());
    List lista = query.list();
    session.close();
    return lista;
    
    }

    @Override
    public void update(Project entity) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void remove(Project entity) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean consult(String name) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession sessions = (HttpSession) fc.getExternalContext().getSession(false);
        User u = (User) sessions.getAttribute("user");
        Query query= session.createSQLQuery("select * from Project where name = :name and user_id = :user_id").addEntity(Project.class);
        query.setParameter("name",name);
        query.setParameter("user_id", u.getId());
        List lista = query.list();
        if(lista == null || lista.isEmpty()){
            return true;
        }else{
            return false;
        }
    }

}
